from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession

from src.application.payment.enums import PaymentStatus
from src.infrastructure.db.models import Payment
from src.infrastructure.db.services.users import get_user


async def get_payment(session: AsyncSession, payment_id: int) -> Payment:
    q = select(Payment).where(Payment.id == payment_id)
    res = await session.execute(q)

    return res.scalar()


async def create_payment(session: AsyncSession, summ: float, user_id: int) -> Payment:
    user = await get_user(session, user_id)

    payment = Payment(summ=summ, user=user, user_id=user_id)
    session.add(payment)

    await session.commit()

    return payment


async def update_payment(session: AsyncSession, payment_id: int, status: PaymentStatus) -> None:
    q = update(Payment).where(Payment.id == payment_id).values(status=status)

    await session.execute(q)
    await session.commit()
